package pnu.ssm.ga;

public class Individual {
	
	private static int defaultGeneLength = -1; 
	private static int maxiGeneNum = -1;
	
	private int genes[];
	private int fitness = 0;
	
	public void generateIndividual(){
		if(defaultGeneLength < 0 || maxiGeneNum < 0) {
			System.err.println("ERROR : You have to set size of job and transporter bigger than zero.");
			// 아마 에러가 출력될 일은 없을 것.
			System.exit(0);
		}
		initIndividual();
		for(int i=0; i<size(); i++){
			int gene = (int) (Math.random() * maxiGeneNum); 
			// gene은 0 부터 Transporter 개수 - 1 까지 나온다.
			genes[i] = gene;
		}
	}
	
	public void initIndividual(){
		genes = new int[defaultGeneLength];
	}
	
	public static void setDefaultGeneLength(int length){
		defaultGeneLength = length;
	}
	
	public static void setMaximumGeneNumber(int length){
		maxiGeneNum = length;
	}
	
	public static int getMaximumGeneNumber(){
		return maxiGeneNum;
	}
	
	public int getGene(int index){
		return genes[index];
	}
	
	public void setGene(int index, int value){
		genes[index] = value;
		fitness = 0;
	}
	
	public int size(){
		return genes.length;
	}
	
	public int getFitness(){
		if(fitness == 0){
			fitness = FitnessCalc.getFitness(this);
		}
		return fitness;
	}
	
	@Override
	public String toString() {
		String geneString = "";
		for(int i=0; i<size(); i++){
			geneString += getGene(i);
		}
		return geneString;
	}
}

